package com.tiemei.demo.client.tair;

import java.io.Serializable;

import net.sf.json.JSONObject;

import com.taobao.tair.DataEntry;
import com.taobao.tair.Result;
import com.taobao.tair.ResultCode;
import com.taobao.tair.impl.mc.MultiClusterTairManager;

/**
 * Created by IntelliJ IDEA. User: qingli.oyr Date: 12-9-28 Time: ����4:35 To
 * change this template use File | Settings | File Templates.
 */
public class DailyTairFeatureV1 {
	static int namespace = 16;
	static MultiClusterTairManager mcTairManager;

	public static void main(String[] args) throws InterruptedException {

		mcTairManager = new MultiClusterTairManager();
		mcTairManager.setConfigID("mdbcomm-daily");
		mcTairManager.setDynamicConfig(true);
		mcTairManager.init();

		/*
		 * 1500011742209 1500013255682
		 * ��tridentϵͳ����Ŀ¼��\features\src\main\resources\features-beans.xml�в���
		 * <property name="prefix" value="features_discounttime" />�Դ����� �°�
		 * features_discounttime features_feedback features_ump features_dsr �ϰ棺
		 * ic_features rc_features ump_features
		 */
		// String key = "features_feedback$1500017004124";
		// String key1 = "features_feedback$40960000000001";
		// String key2 = "features_feedback$30720000000001";
		// String key3 = "features_feedback$20480000000001";
		// String key4 = "features_feedback$1500011376000";
		// String key1 = "features_ump$1500002607104"; //1500017004124
		// String key4 = "features_feedback$1500002607104";
		String key2 = "features_dsr$2062279930";
		// String key3 = "features_feedback$1500017004124";
		// String key5 = "features_ump$1500017004124";
		// String key = "features_dsr$2050919495";
		String umpValue = "{\"promoName\":\"著名折扣\",\"promoPrice\":\"70.00\",\"promoOtherNeed\":\"金币200\"}";
		String dsrValue = "{\"dsrInd\":\"服装/服装\",\"dsrDescribe\":\"4.0\",\"dsrDescribeGap\":\"87.64\",\"dsrService\":\"4.8\",\"dsrServiceGap\":\"89.11\",\"dsrDeliver\":\"4.8\",\"dsrDeliverGap\":\"86.25\"}";
		String dsrValue2 = "{\"dsrInd\":\"不同的ind\",\"dsrDescribe\":\"4.0\",\"dsrDescribeGap\":\"87.64\",\"dsrService\":\"4.8\",\"dsrServiceGap\":\"89.11\",\"dsrDeliver\":\"4.8\",\"dsrDeliverGap\":\"86.25\"}";

		Long expired = System.currentTimeMillis() / 1000 + 600;
		
		
//		deleteTair("features_ump$1500002607104");
//		deleteTair("features_ump$1500017004124");
//		addTair(key2, dsrValue, expired);
//		queryTair(key2);
//		addTair(key2, dsrValue2, expired);
//		queryTair(key2);
		// "1500002607104", "1500017004124"
		queryTair("features_ump$1500002607104");
		queryTair("features_ump$1500017004124");
		queryTair("features_feedback$1500002607104");
		queryTair("features_feedback$1500017004124");
//		deleteTair("features_feedback$1500017004124");
	}

	public static void deleteTair(String key) {
		ResultCode code = mcTairManager.delete(namespace, key);
		if (code.isSuccess()) {
			System.out.println("delete success! key = " + key);
		}
	}

	public static void addTair(String key, String value, Long expired) {
		CacheEntry entry = new CacheEntry();
		entry.setKey(key);
		entry.setValue(value);
		entry.setExpireTime(expired);
		String prefix = key.split("\\$")[0];
		String clazz = null;
		if ("features_ump".equals(prefix)) { // 优惠
			clazz = "UmpFeatureDo";
		} else if ("features_dsr".equals(prefix)) { // dsr
			clazz = "DsrFeatureDo";
		} else if ("features_udc".equals(prefix)) { // udc
			clazz = "UdcFeatureDo";
		} else if ("features_feedback".equals(prefix)) { // 评价
			clazz = "RcFeatureDo";
		} else { // wrong key
			System.err.printf("wrong key : " + key, " , add failed!");
			return ;
		}
		entry.setClazz(clazz);
		ResultCode code = mcTairManager.put(namespace, key, JSONObject
				.fromObject(entry).toString(), 0, 86400000);
		if (code.isSuccess()) {
			System.out.println("add sucess! key = " + key);
		}
	}

	public static void queryTair(String key) {
		Result<DataEntry> result = mcTairManager.get(namespace, key);
		if (result.isSuccess() && result.getValue() != null) {
			System.out.println("query success! return = " + result.getValue().getValue());
		} else
			System.out.println("query tair fail!");
	}

	public static class CacheEntry implements Serializable {

		private static final long serialVersionUID = 1L;

		private Serializable key;
		private Serializable value;
		private String clazz;
		private long expireTime;

		public Serializable getKey() {
			return key;
		}

		public void setKey(Serializable key) {
			this.key = key;
		}

		public Serializable getValue() {
			return value;
		}

		public void setValue(Serializable value) {
			this.value = value;
		}

		public long getExpireTime() {
			return expireTime;
		}

		public void setExpireTime(long expireTime) {
			this.expireTime = expireTime;
		}

        public String getClazz() {
        	return clazz;
        }

        public void setClazz(String clazz) {
        	this.clazz = clazz;
        }
		
	}

}
